package com.example.springjpa.sender;

import com.example.springjpa.rabbitmq.ConfirmCallbackService;
import com.example.springjpa.rabbitmq.ReturnCallbackService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: zsw
 * @time: 2021/7/31 11:04
 */
@Component
public class SendRabbitMq {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ConfirmCallbackService confirmCallbackService;
    @Autowired
    private ReturnCallbackService returnCallbackService;

    public void test(String message) {
        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setConfirmCallback(confirmCallbackService);
        /*** 消息投递到队列失败回调处理 */
        rabbitTemplate.setReturnCallback(returnCallbackService);
        //发送消息
        rabbitTemplate.convertAndSend("confirmTestExchange", "test", message);
    }

}
